package com.yan.movie.services;

import com.yan.movie.dao.Dao;
import com.yan.movie.domain.Movie;
import org.w3c.dom.ls.LSOutput;

import java.util.*;

/**
 * @author taoge
 * @Date 2024/8/1 16:58
 * @Description
 */
public class ServiceImpl implements Service{
    public static final Scanner INPUT = new Scanner(System.in);
    public static final Dao DAO = new Dao();

    @Override
    public void addMovie() {
        System.out.println("请输入电影名称");
        String name = INPUT.next();
        System.out.println("请输入电影类型");
        String type = INPUT.next();
        System.out.println("请输入电影人气");
        int i = INPUT.nextInt();
        Movie movie = new Movie(name, type);
        DAO.map.put(movie , i);
        System.out.println("添加成功");
        System.out.println();
    }

    @Override
    public void viewMovie() {
        Set<Movie> movies = DAO.map.keySet();
        movies.forEach(item-> System.out.println(item + "电影人气:" + DAO.map.get(item) + "\n"));
        System.out.println();
    }

    @Override
    public void mostPopularMovie() {
        /*Set<Movie> movies = Dao.map.keySet();
        ArrayList<Integer> list1 = new ArrayList<>();
        ArrayList<Movie> list2 = new ArrayList<>();
        for (Movie movie : movies) {
            list1.add(Dao.map.get(movie));
            list2.add(movie);
        }
        int index = 0;
        for (int i = 1; i < list1.size(); i++) {
            if(list1.get(i) > list1.get(i-1)){
                index = i;
            }
        }
        System.out.println(list2.get(index) +""+ list1.get(index));*/
        Integer maxPopular = 0;
        Collection<Integer> values = Dao.map.values();
        for (Integer value : values) {
            if(value > maxPopular){
                maxPopular = value;
            }
        }
        Set<Movie> movies = Dao.map.keySet();
        for (Movie movie : movies) {
            if(Dao.map.get(movie).equals(maxPopular)){
                System.out.println(movie + "电影人气:" + maxPopular);
            }
        }
    }
}
